###################################################
# Party Fragmentation and Campaign Spending: A
# Subnational Analysis of the German Party System
#
# Marc S. Jacob & Jan Pollex
# Party Politics
###################################################

# Set working directory ---------------------------------------------------

setwd(
  "C:/Users/majacob/Dropbox/Jan_Marc_paper/Party Politics/Final/replication"
)

# Load libraries ----------------------------------------------------------

library(tidyverse)
library(reshape2)
library(ggpubr)
library(sjPlot)
library(gridExtra)
library(glmmTMB)

# Figure 1 ----------------------------------------------------------------

# loading data

data <- read.csv(file = "data_heatmap.csv", na.strings = c("", "NA"))

data$expenses <- data$e_staff + data$e_mat + data$e_pol_lab + data$e_camp
data$apparatus <-
  (data$e_staff + data$e_mat + data$e_pol_lab) / data$expenses * 100

Greens <- data[which(data$party == 'Greens'), ]
Greens$expenses <- Greens$apparatus
Greens <- aggregate(expenses ~ state, data = Greens, FUN = sum)
Greens$expensesrel_av <- Greens$expenses / 9
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- Greens$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
Greens <-
  spplot(my.data,
         zcol = "clicks",
         main = "Greens",
         col.regions = gray(seq(1, 0, by = -0.01)))
Greens

###CDU###

CDU <- data[which(data$party == 'CDU'), ]
CDU$expenses <- CDU$apparatus
CDU <- aggregate(expenses ~ state, data = CDU, FUN = sum)
CDU$expensesrel_av <- CDU$expenses / 9
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- CDU$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
CDUCSU <-
  spplot(my.data,
         zcol = "clicks",
         main = "CDU/CSU",
         col.regions = gray(seq(1, 0, by = -0.01)))
CDUCSU

###SPD###

SPD <- data[which(data$party == 'SPD'), ]
SPD$expenses <- SPD$apparatus
SPD <- aggregate(expenses ~ state, data = SPD, FUN = sum)
SPD$expensesrel_av <- SPD$expenses / 9
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- SPD$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
SPD <-
  spplot(my.data,
         zcol = "clicks",
         main = "SPD",
         col.regions = gray(seq(1, 0, by = -0.01)))
SPD


###Left###
Left <- data[which(data$party == 'Left'), ]
Left$expenses <- Left$apparatus
Left <- aggregate(expenses ~ state, data = Left, FUN = sum)
Left$expensesrel_av <- Left$expenses / 9
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- Left$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
Left <-
  spplot(my.data,
         zcol = "clicks",
         main = "Left",
         col.regions = gray(seq(1, 0, by = -0.01)))
Left


###FDP###

FDP <- data[which(data$party == 'FDP'), ]
FDP$expenses <- FDP$apparatus
FDP <- aggregate(expenses ~ state, data = FDP, FUN = sum)
FDP$expensesrel_av <- FDP$expenses / 9
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- FDP$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
FDP <-
  spplot(my.data,
         zcol = "clicks",
         main = "FDP",
         col.regions = gray(seq(1, 0, by = -0.01)))
FDP

###AfD###

AfD <- data[which(data$party == 'AfD'), ]
AfD$expenses <- AfD$apparatus
AfD <- aggregate(expenses ~ state, data = AfD, FUN = sum)
AfD$expensesrel_av <- AfD$expenses / 5
sample <- data.frame(
  HASC_1 =    c(
    "DE.BR",
    "DE.BE",
    "DE.BW",
    "DE.BY",
    "DE.HB",
    "DE.HE",
    "DE.HH",
    "DE.MV",
    "DE.NI",
    "DE.NW",
    "DE.RP",
    "DE.SH",
    "DE.SL",
    "DE.SN",
    "DE.ST",
    "DE.TH"
  ),
  clicks = NA
)
sample$clicks <- AfD$expensesrel_av

my.data <- readRDS("Germany_map.rds")
sample$HASC_1 <- as.character(sample$HASC_1)
final <-
  merge(
    x = my.data@data,
    y = sample,
    by = "HASC_1",
    all.y = TRUE
  )
my.data@data <- data.frame(my.data@data,
                           sample[match(my.data@data[, "HASC_1"],
                                        sample[, "HASC_1"]), ])
AfD <-
  spplot(my.data,
         zcol = "clicks",
         main = "AfD",
         col.regions = gray(seq(1, 0, by = -0.01)))
AfD

## Save plot

pdf(
  file = "heatmap.pdf",
  width = 8,
  height = 5,
  onefile = F
)

grid.arrange(CDUCSU, SPD, Left, Greens, FDP, AfD, nrow = 2)

dev.off()

# Figure 2 ----------------------------------------------------------------

data <-
  read.csv(file = "data_regression.csv", na.strings = c("", "NA"))

# State election years
e_camp_election <-
  aggregate(data$e_camp, by = list(data$party, data$state, data$state_el), mean)
e_camp_ENP <-
  aggregate(data$ENP, by = list(data$party, data$state), mean)

data_wide <-
  dcast(e_camp_election,
        Group.1 + Group.2 ~ Group.3,
        value.var = "x",
        drop = T)

data_wide$ratio <- data_wide$`1` / (data_wide$`0` + data_wide$`1`)

merged <- merge(data_wide, e_camp_ENP, by = c("Group.1", "Group.2"))

state <-
  ggplot(merged, aes(x = ratio, y = x, shape = Group.1)) + geom_point() +  geom_smooth(method =
                                                                                         lm, aes(group = 1)) + ylab("Average ENEP") + xlab("Ratio state election year spending") + labs(shape = "Party") + theme_bw()

# Federal election years

e_camp_election <-
  aggregate(data$e_camp,
            by = list(data$party, data$state, data$federal_el),
            mean)
e_camp_ENP <-
  aggregate(data$ENP, by = list(data$party, data$state), mean)

e_camp_election$ENP <- e_camp_ENP$x

data_wide <-
  dcast(e_camp_election,
        Group.1 + Group.2 ~ Group.3,
        value.var = "x",
        drop = T)

data_wide$ratio <- data_wide$`1` / (data_wide$`0` + data_wide$`1`)

merged <- merge(data_wide, e_camp_ENP, by = c("Group.1", "Group.2"))

federal <-
  ggplot(merged, aes(x = ratio, y = x, shape = Group.1)) + geom_point() +  geom_smooth(method =
                                                                                         lm, aes(group = 1)) + ylab("Average ENEP") + xlab("Ratio federal election year spending") + labs(shape = "Party") + theme_bw()

## Save plot

pdf(
  file = "Figure_ratio.pdf",
  width = 10,
  height = 5,
  onefile = F
)

ggarrange(
  state,
  federal,
  ncol = 2,
  nrow = 1,
  common.legend = TRUE,
  legend = "bottom",
  labels = c("a)", "b)")
)

dev.off()

# Table 1 -----------------------------------------------------------------

# prepare data

data$income <- data$i_fees + data$i_don_ind + data$i_don_cor
data$camp_10000 <- data$e_camp / 10000

# set variables to factors

data$fed_opp <-
  car::recode(data$federal_gov, "0=1;1=0", as.factor = TRUE)
data$state_gov <- as.factor(data$state_gov)
data$state_el <- as.factor(data$state_el)
data$federal_el <- as.factor(data$federal_el)

# round DV (for negative binomial model)

data$e_camp <- round(data$e_camp, digits = 0)

# rescale IVs

rescale <- function(x)
  (x - min(x)) / (max(x) - min(x)) * 100

data$income <-  rescale(data$income)
data$income <- round(data$income, digits = 0)

# transform ENP

data$ENPx10 <-
  data$ENP * 10 # multiply ENP times 10 
data$ENPx10 <- round(data$ENPx10, digits = 0) # and round to receive integers

# build dataframe without AfD and The Left observations

datamAfD <- data[!(data$party == "AfD"), ]
datamLeft <- data[!(data$party == "Left"), ]

### Models: campaigning ###

# Model 1 - without interactions

model_camp1 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + income +  (1 |
                                                                                     state:party) + (1 |
                                                                                                       year) + (1 |
                                                                                                                  state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )

# Model 2 - with interactions

model_camp2 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )

# Model 3 - without AfD observations

model_camp3 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamAfD
  )

# Model 4 - without The Left observations

model_camp4 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income + (1 |
                  state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamLeft
  )


apl <- c(
  `(Intercept)` = "Intercept",
  ENPx10 = "ENPx10",
  state_gov1 = "State government",
  federal_gov = "Federal government",
  state_el1 = "State election",
  federal_el1 = "Federal election",
  income = "Income",
  `ENPx10:state_el1` = "(ENEPx10)xState election",
  `ENPx10:federal_el1` = "(ENEPx10)xFederal election"
)

## Table models

tab_model(
  model_camp1,
  model_camp2,
  model_camp3,
  model_camp4,
  show.ci = 0.95,
  show.p = FALSE,
  transform = NULL,
  show.std = "std2",
  show.zeroinf = TRUE,
  show.est = FALSE,
  show.se = TRUE,
  collapse.se = TRUE,
  digits = 3,
  p.style = "stars",
  dv.labels = c(
    "Model 1",
    "Model 2",
    "Model 3 (without AfD)",
    "Model 4 (without Left)"
  ),
  pred.labels = apl
)

# Figure 3 ----------------------------------------------------------------

options(scipen = 10000)

margeff_model_camp <-
  ggeffects::ggpredict(model_camp2, c("ENPx10 [all]", "state_el"))
margeff_model_camp1 <-
  plot(margeff_model_camp,
       colors = "bw",
       show.legend = TRUE) + labs(
         x = "ENEP*10",
         y = "Campaign expenditure in euro",
         title = "",
         linetype  = "State election"
       ) + scale_linetype_manual(values = 15:17, labels = c("no", "yes"))

margeff_model_camp <-
  ggeffects::ggpredict(model_camp2, c("ENPx10 [all]", "federal_el"), add.data =
                         TRUE)
margeff_model_camp2 <-
  plot(margeff_model_camp,
       colors = "bw",
       show.legend = TRUE) + labs(
         x = "ENEP*10",
         y = "Campaign expenditure in euro",
         title = "",
         linetype  = "Federal election"
       ) + scale_linetype_manual(values = 15:17, labels = c("no", "yes"))
margeff_model_camp2

pdf(file = "marg_eff.pdf",
    width = 10,
    height = 5,onefile=F)

cowplot::plot_grid(
  margeff_model_camp1,
  margeff_model_camp2,
  labels = c("a)", "b)"),
  ncol = 2,
  nrow = 1
)

dev.off()

